package com.stars.empoly.utils

import android.content.Context
import android.text.TextUtils
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import android.widget.LinearLayout
import android.widget.TextView
import android.widget.Toast
import com.qmuiteam.qmui.kotlin.onClick
import com.qmuiteam.qmui.layout.QMUIFrameLayout
import com.qmuiteam.qmui.skin.QMUISkinValueBuilder
import com.qmuiteam.qmui.util.QMUIDisplayHelper
import com.qmuiteam.qmui.widget.popup.QMUIPopup
import com.qmuiteam.qmui.widget.popup.QMUIPopups
import com.stars.empoly.R
import com.stars.empoly.inte.ChoiceData
import com.stars.empoly.inte.ChoiceListData
import com.stars.empoly.widget.TimeDayView
import com.stars.empoly.widget.TimeLeftView
import com.stars.empoly.widget.TimeRightView
import java.text.SimpleDateFormat
import java.util.*

/**
 * 选择时间
 *
 *on 2020/11/21
 */
object PopuChoseTime {




    fun show(context: Context, view: View,choiceCallData: ChoiceListData) {
        var time1 = ""
        var time2 = ""

        var mNormalPopup = QMUIPopups.fullScreenPopup(context)
        val builder: QMUISkinValueBuilder = QMUISkinValueBuilder.acquire()
        val frameLayout = QMUIFrameLayout(context)
        //自定义布局
        val layoutInflater = context?.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        var layout = layoutInflater.inflate(R.layout.item_chose_time, null)
        val  ll:LinearLayout = layout.findViewById(R.id.ll)

        val llData: LinearLayout = layout.findViewById(R.id.ll_day)
        val tvData: TextView = layout.findViewById(R.id.tv_day)
        val lineData: View = layout.findViewById(R.id.line_day)
        val llTime: LinearLayout = layout.findViewById(R.id.ll_time)
        val tvTime: TextView = layout.findViewById(R.id.tv_time)
        val lineTime: View = layout.findViewById(R.id.line_time)
        val tvSure: TextView = layout.findViewById(R.id.tv_sure)

        val timeLeft: TimeLeftView = layout.findViewById(R.id.time_left)

        val timeDay: TimeDayView = layout.findViewById(R.id.time_day)


        time1 = ""//年月日
        time2 = ""//年月


        timeLeft.setCallBack(object : ChoiceListData {
            override fun onRestuse(code: String?, name: String?) {

                time2 = name!!

                tvData.text = time2

                time1=""



            }
        })


        timeDay.setCallBack(object : ChoiceListData {
            override fun onRestuse(code: String?, name: String?) {

                time1 = name!!
                tvTime.text = time1
                time2=""

            }
        })

        tvData.text="日期"
        time2 = ""//年月日


        //        time1 = ""//年月日
//        time2 = ""//年月

        tvSure.onClick {

            if(TextUtils.isEmpty(time1)&&TextUtils.isEmpty(time2)){
                Toast.makeText(context, "请选择", Toast.LENGTH_SHORT).show()
                return@onClick
            }

            if(null!=choiceCallData) {
                if (!TextUtils.isEmpty(time1)) {
                    choiceCallData!!.onRestuse(time1,"0")
                }else{
                    choiceCallData!!.onRestuse(time2,"1")
                }

            }

            mNormalPopup?.dismiss()
        }



        llData.onClick {
            lineData.visibility = View.VISIBLE
            lineTime.visibility = View.INVISIBLE
            timeLeft.visibility = View.VISIBLE
            timeDay.visibility = View.GONE
            tvTime.text="月份"
            time2=""

            time1 = timeLeft.getTimeContent()
            tvData.text = time1
        }
        llTime.onClick {
            lineTime.visibility = View.VISIBLE
            lineData.visibility = View.INVISIBLE
            timeDay.visibility = View.VISIBLE
            timeLeft.visibility = View.GONE
            tvData.text="日期"
            time1=""

            time2 = timeDay.getTimeContent()
            tvTime.text = time2


//            if(TextUtils.isEmpty(time2)){
//                time2 = timeDay.getTime()
//                tvTime.text = time2
//            }
        }

        ll.onClick {

            mNormalPopup?.dismiss()
        }

        builder.release();

        val size = QMUIDisplayHelper.getScreenWidth(context)
        val height = QMUIDisplayHelper.getScreenHeight(context)


        val lp: FrameLayout.LayoutParams = FrameLayout.LayoutParams(size,
            height)
        frameLayout.addView(layout, lp)

        mNormalPopup.addView(frameLayout)
        mNormalPopup.onDismiss() {
        }

        mNormalPopup.dismissIfOutsideTouch(false)
        mNormalPopup.animStyle(QMUIPopup.ANIM_AUTO)
        mNormalPopup.show(view)


    }


}
